package service.impl;

import global.Constants;
import global.security.SessionUtils;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import service.AlbumService;
import service.PictureService;
import util.StringUtils;
import dao.AlbumDAO;
import domain.Album;
import domain.Picture;
import domain.PictureWithBLOBs;

/**
 * 相册Service实现类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class AlbumServiceImpl implements AlbumService {

	/**
	 * 相册Dao
	 * */
	private AlbumDAO albumDao = null;
	
	/**
	 * 图片Service
	 * */
	private PictureService pictureService = null;

	/**
	 * 设定相册Dao
	 * */
	public void setAlbumDao(AlbumDAO albumDao) {
		this.albumDao = albumDao;
	}
	
	/**
	 * 设定图片Service
	 * @param pictureService
	 *            the pictureService to set
	 */
	public void setPictureService(PictureService pictureService) {
		this.pictureService = pictureService;
	}

	/**
	 * 查询分类数量
	 * @param paramAlbum 相册
	 * @return int 分类数量
	 */
	public int countByCriteria(Album paramAlbum) {
		paramAlbum = getConditions(paramAlbum);
		int count = albumDao.countByCriteria(paramAlbum);
		return count;
	}
	
	/**
	 * 获得相册
	 * @param albumId 相册ID
	 * @return Album 相册
	 */
	public Album selectByPrimaryKey(Integer albumId){
		return albumDao.selectByPrimaryKey(albumId);
	}

	/**
	 * 查询相册列表
	 * @param paramAlbum 相册
	 * @return List 相册列表
	 */
	public List<Album> selectByCriteria(Album paramAlbum) {
		// 查询相册列表
		paramAlbum = getConditions(paramAlbum);
		List<Album> albumList = albumDao.selectByCriteria(paramAlbum);
		if (albumList != null && albumList.size() != 0) {
			return albumList;
		} else {
			return null;
		}
	}
	
	/**
	 * 查询相册树
	 * @param parentId 相册ID
	 * @return JSONArray 相册树
	 */
	public JSONArray selectAlbumForTree(String parentId){
		JSONArray res = new JSONArray();
		// 设定条件
		Album paramAlbum = new Album();
		if(StringUtils.isNotEmpty(parentId)){
			paramAlbum.setParentId(Integer.parseInt(parentId));
		}
		// 取得笔记分类列表
		List<Album> albumList = new ArrayList<Album>();
		albumList = this.selectByCriteria(paramAlbum);
		res = getAlbumTreeFromList(albumList, parentId);
		return res;
	}

	/**
	 * 添加相册
	 * @param paramAlbum 相册
	 */
	public void insert(Album album) {
		// 删除区分：未删除
		album.setDelflag(Constants.DEL_FLAG_1);
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		album.setCreateUser(SessionUtils.getCurrentUserId());
		album.setCreateTime(sysdate);
		album.setUpdateUser(SessionUtils.getCurrentUserId());
		album.setUpdateTime(sysdate);
		albumDao.insert(album);
	}

	/**
	 * 修改相册
	 * @param paramAlbum 相册
	 */
	public void update(Album album) {
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		album.setUpdateUser(SessionUtils.getCurrentUserId());
		album.setUpdateTime(sysdate);
		albumDao.updateByPrimaryKey(album);
	}

	/**
	 * 删除相册
	 * @param albumId 相册ID
	 * @param uploadFilePath 图片上传的路径
	 */
	public void delete(int albumId, String uploadFilePath) {
		// 取得相册列表
		List<Album> albumList = new ArrayList<Album>();
		// 递归查询相册
		Album parentAlbum = new Album();
		if(0 == albumId){// 根目录
			parentAlbum.setAlbumId(albumId);
		}else{
			parentAlbum = this.selectByPrimaryKey(albumId);
		}
		getAlbumList(parentAlbum, albumList);
		for (Album album : albumList) {
			// 删除相册下的图片
			if("1".equals(album.getIsleaf())){
				// 参数设定
				PictureWithBLOBs paramPicture = new PictureWithBLOBs();
				paramPicture.setAlbumId(album.getAlbumId());
				paramPicture.setDelflag(Constants.DEL_FLAG_1);
				
				// 取得图片列表
				List<Picture> pictureList = pictureService.selectByCriteria(paramPicture);
				if (pictureList != null && pictureList.size() > 0) {
					for (Picture picture : pictureList) {
						// 删除磁盘图片-大图
						File lfile = new File(uploadFilePath + "/" + picture.getLpath());
						if (null != lfile) {
							lfile.delete();
						}
						// 删除磁盘图片-缩略图
						File sfile = new File(uploadFilePath + "/" +Constants.THUMBNAIL + "/" + picture.getSpath());
						if (null != sfile) {
							sfile.delete();
						}
					}
				}
			}
			// 删除相册
			albumDao.deleteByPrimaryKey(album.getAlbumId());
		}
	}

	/**
	 * 返回笔记树
	 * 
	 * @param albumList
	 * @return
	 */
	private JSONArray getAlbumTreeFromList(List<Album> albumList, String parentId) {
		JSONArray results = new JSONArray();
		
		if("-1".equals(parentId)){
			JSONObject allAlbum = new JSONObject();
			allAlbum.put("id", "allPicture");
			allAlbum.put("text", "所有图片");
			allAlbum.put("leaf", true);
			results.add(allAlbum);
			
			JSONObject album = new JSONObject();
			album.put("id", "0");
			album.put("text", "我的相册");
			album.put("leaf", false);
			results.add(album);
		}else{
			if (null != albumList) {
				for (int i = 0; i < albumList.size(); i++) {
					JSONObject albumNode = new JSONObject();
					albumNode.put("id", albumList.get(i).getAlbumId());
					albumNode.put("text", albumList.get(i).getAlbumName());
					albumNode.put("leaf", Constants.ISLEAF_MAP.get(albumList.get(i).getIsleaf()));
					results.add(albumNode);
				}
			}
		}
		return results;
	}
	
	/**
	 * 递归查询相册
	 * 
	 * @param albumId 相册ID
	 * @param albumList 相册列表
	 * @return
	 */
	private void getAlbumList(Album parentAlbum, List<Album> albumList){
		// 添加本相册
		albumList.add(parentAlbum);
		// 递归查询子相册
		Album paramAlbum = new Album();
		paramAlbum.setParentId(parentAlbum.getAlbumId());
		List<Album> newAlbumList = this.selectByCriteria(paramAlbum);
		if(null != newAlbumList){
			for(int i=0;i<newAlbumList.size();i++){
				getAlbumList(newAlbumList.get(i), albumList);
			}
		}
	}
	
	/**
	 * 设置公共参数
	 * @param parentAlbum 参数对象
	 */
	private Album getConditions(Album parentAlbum){
		parentAlbum.setCreateUser(SessionUtils.getCurrentUserId());// 当前用户
		return parentAlbum;
	}
}
